import Vue from 'vue';
import VueRouter from 'vue-router';
import Main from '../views/Main.vue';
// import validateToken from '../utils/validateToken';


const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
}

Vue.use(VueRouter)

const routes = [{
    path: '/ycwAdmin/main',
    name: 'Main',
    component: Main,
    meta: {
      auth: true
    },
    children:[{
      path: 'user',
      component: ()=>import('../views/main/user.vue')
    },{
      path: 'attraction',
      component: ()=>import('../views/main/attraction.vue')
    },{
      path: 'product',
      component: ()=>import('../views/main/product.vue')
    },{
      path: 'room',
      component: ()=>import('../views/main/room.vue')
    },{
      path: 'carousel',
      component: ()=>import('../views/main/carousel.vue')
    },{
      path: 'coupon',
      component: ()=>import('../views/main/coupon.vue')
    },{
      path: 'message',
      component: ()=>import('../views/main/message.vue')
    },{
      path: 'admin',
      component: ()=>import('../views/main/admin.vue')
    }]
  },
  {
    path: '/ycwLogin',
    name: 'Login',
    component: () => import( /* webpackChunkName: "about" */ '../views/Login.vue'),
    meta: {
      auth: false
    }
  }
]
const router = new VueRouter({
  routes
})

router.beforeEach(async (to, from, next) => {
  if (to.meta.auth) { //权限判断
    //读取token值
    if (localStorage.getItem('token')) {
      //  成功
      // console.log(1233);
      
      next()
    } else {
      next({
        path: '/ycw_login'
      })
    }
  } else {
    // 没有meta.auth 不用管
    next()
  }

});


export default router